package cn.cycleif.util;

import cn.cycleif.enums.ResultCodeEnum;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import static cn.cycleif.enums.ResultCodeEnum.ERROR;
import static cn.cycleif.enums.ResultCodeEnum.SUCCESS;

/**
 * &#064;Date: 24/04/22-17:58
 * &#064;Description: 接口返回值
 */
@Data
public class R<T> {
  @Schema(name = "code", description = "状态码")
  private Integer code;

  @Schema(name = "msg", description = "返回消息")
  private String msg;

  @Schema(name = "data", description = "返回数据")
  private T data;

  public static <T> R<T> success() {
    return new R<>(SUCCESS.code, SUCCESS.desc, null);
  }

  public static <T> R<T> error(String msg) {
    return new R<>(ERROR.code, msg, null);
  }

  public static <T> R<T> success(T object) {
    return new R<>(SUCCESS.code, SUCCESS.desc, object);
  }

  public static <T> R<T> success(T object, String msg) {
    return new R<>(SUCCESS.code, msg, object);
  }

  public static <T> R<T> error() {
    return R.error(ERROR);
  }

  public static <T> R<T> error(ResultCodeEnum resultCodeEnum) {
    return new R<>(resultCodeEnum.code, resultCodeEnum.desc, null);
  }

  public R(Integer code, String msg, T data) {
    this.code = code;
    this.msg  = msg;
    this.data = data;
  }


}

